package com.milhouz.findmydroid;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.milhouz.findmydroid.bean.Character;
import com.milhouz.findmydroid.db.AppDbAPI;
import com.milhouz.findmydroid.view.CustomCharsAdapter;

public class CustomCharsActivity extends Activity {

	private ListView mCharList;
	private CustomCharsAdapter mAdapter;
	private ImageView addButton;
	private EditText addedCharacter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Set the fullscreen mode
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.customchars);

		// Get views
		mCharList = (ListView) findViewById(R.id.customCharList);
		addButton = (ImageView) findViewById(R.id.addButton);
		addedCharacter = (EditText) findViewById(R.id.addedChar);

		// Get user characters
		List<Character> charactersList = AppDbAPI.getInstance().getUserCharacters(getApplicationContext());

		// Create the list adapter
		mAdapter = new CustomCharsAdapter(this, charactersList);
		mCharList.setAdapter(this.mAdapter);

		// Add button listener
		addButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String addedChar = addedCharacter.getText().toString();
				if (addedChar != null && !TextUtils.isEmpty(addedChar.trim())) {
					// Save the character
					AppDbAPI.getInstance().insertCharacter(getApplicationContext(), addedChar);
					mAdapter.updateItem(new Character(addedChar));
					addedCharacter.setText("");
				}
			}
		});

	}

}
